//
//  WYMessageController.m
//  Boss
//
//  Created by admin on 2018/7/3.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMessageController.h"
#import "WYMessageCell.h"
#import "UIView+Extension.h"
#import "WYMessageDetailController.h"
#import "WYMessageModel.h"
@interface WYMessageController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)BOOL refresh;
@property (nonatomic,assign)NSUInteger page;
@end

@implementation WYMessageController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
    
    self.refresh = NO;
    self.page = 1;
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        weakSelf.page++;
        // [weakSelf loadTableData];
        [weakSelf loadData];
    }];
    
    
    
    
}
-(void)loadData
{
     NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    WEAKSELF
    [WYHttpServer getMessageListWithCurrentPage:page SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            [weakSelf.dataArray addObjectsFromArray:[WYMessageModel getMessageModelFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
                
                
                
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }else{
            
        }
        
        
    }];
}
-(void)setupUI
{
    self.navigationItem.title = @"系统消息";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"全部已读" style:(UIBarButtonItemStyleDone) target:self action:@selector(allRead)];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYMessageCell" bundle:nil] forCellReuseIdentifier:@"WYMessageCell"];
    self.tableView.rowHeight = 268;
    self.tableView.backgroundColor = [UIColor hexStringToColor:@"#f7f7f7" alpha:1.0];
}

-(void)allRead
{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"确定全部已读？" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self sureAllRead];
        //        WYReportController *rpc = [[WYReportController alloc]init];
        //
        //        [self.navigationController pushViewController:rpc animated:YES];
    }];
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
    
}
-(void)sureAllRead
{
    [WYHttpServer cleanSystemMessageSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            
        }
        
        
    }];
}







-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYMessageCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYMessageCell" owner:nil options:nil].firstObject;
    }
    WYMessageModel *model = self.dataArray[indexPath.section];
    [cell setModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYMessageModel *model = self.dataArray[indexPath.section];
    WYMessageDetailController *mdvc = [[WYMessageDetailController alloc]init];
    mdvc.msgID = model.msgId;
    [self.navigationController pushViewController:mdvc animated:YES];
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 0.000001;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 0.000001;
//}
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *view = [[UIView alloc]init];
//    //view.backgroundColor = kColorMainColor;
//    UILabel *label = [[UILabel alloc]init];
//    [view addSubview:label];
//     WYMessageModel *model = self.dataArray[section];
//    label.text = model.releaseTime ;
//    [label mas_makeConstraints:^(MASConstraintMaker *make) {
//       // make.top.mas_equalTo(5);
//        make.center.mas_equalTo(view);
//    }];
//    label.font = [UIFont systemFontOfSize:15];
//    label.textColor = [UIColor hexStringToColor:@"#999999" alpha:1.0];
//    return view;
//}







- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}

@end
